package com.iknight.theory.controller;

import com.github.pagehelper.PageInfo;
import com.iknight.mgr.core.base.BaseController;
import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TeLineService;
import com.iknight.theory.model.TELineModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
 * @author JeryZym
 * @date 2017/12/20
 */
@RestController
@RequestMapping("/rest/line")
@Api(value = "线路控制器")
public class LineController implements BaseController<TELineModel> {
    private final static Logger logger = LogManager.getLogger(LineController.class);

    @Autowired
    private TeLineService<TELineModel, String> teLineService;

    @Override
    public TELineModel format(Map<String, Object> params) {
        TELineModel entity = new TELineModel();
        entity.setLine_id(IkgStringUtils.ObjectToString(params.get("line_id")));

        if (IkgStringUtils.isNotNull(params.get("line_no"))) {
            String value = IkgStringUtils.ObjectToString(params.get("line_no"));
            entity.setLine_no(value);
        }
        if (IkgStringUtils.isNotNull(params.get("line_name"))) {
            String line_name = IkgStringUtils.ObjectToString(params.get("line_name"));
            entity.setLine_name(line_name);
        }
        if (IkgStringUtils.isNotNull(params.get("start_pos"))) {
            String start_pos = IkgStringUtils.ObjectToString(params.get("start_pos"));
            entity.setStart_pos(start_pos);
        }

        return entity;
    }

    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存方法", httpMethod = "POST")
    public Object save(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teLineService.save(format(params));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "保存成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/delete")
    @ApiOperation(value = "删除方法", httpMethod = "POST")
    public Object delete(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("line_id"));
            teLineService.delete(pKey);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/selectOne")
    @ApiOperation(value = "单条记录查询方法", httpMethod = "POST")
    public Object selectOne(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("line_id"));
            TELineModel entity = teLineService.selectOne(pKey);
            return IkgWebUtils.postIkgResponse(true, "查询成功!", entity);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
    }

    @Override
    @PostMapping("/list")
    @ApiOperation(value = "根据参数分页查询", httpMethod = "POST")
    public Object selectByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));

        PageInfo<TELineModel> pageInfo = teLineService.selectPage(params, pageIndex, pageSize);
         return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public boolean isPKeyNotNull(Map<String, Object> params) {
        return null != params && IkgStringUtils.isNotNull(params.get("line_id"));
    }
}
